package cn.maihe.elg.operation.supports.electronicseal.anne.service.impl;

import cn.maihe.elg.operation.config.AnneSignConfig;
import cn.maihe.elg.operation.config.ElgConfig;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.enums.ContractStatus;
import cn.maihe.elg.operation.service.order.AcceptOrderContractService;
import cn.maihe.elg.operation.supports.electronicseal.anne.AnneServiceName;
import cn.maihe.elg.operation.supports.electronicseal.anne.BaseAnneSignService;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.base.SecureApiResponse;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.contract.ContractStartSignRequest;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.contract.ContractStartSignRequestBody;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description 发起合同签署
 * @Author WJH
 * @Date 2021/08/13
 */
@Slf4j
@Service
public class ContractStartSignService extends BaseAnneSignService<ContractStartSignRequest, SecureApiResponse> {

    @Resource
    private AcceptOrderContractService acceptOrderContractService;

    public ContractStartSignService(AnneSignConfig anneSignConfig, ElgConfig elgConfig) {
        super(anneSignConfig, elgConfig);
    }

    @Override
    protected AnneServiceName serviceName() {
        return AnneServiceName.contract_start_sign;
    }


    /**
     * 发起合同签署
     * 创建合同-发起合同签署-自动合同签署|手动签署
     *
     * @param anneSignContractId
     */
    public void startSign(Long anneSignContractId) {
        ContractStartSignRequest request = this.buildBaseReq();
        ContractStartSignRequestBody body = new ContractStartSignRequestBody();
        body.setContractIdList(Lists.newArrayList(anneSignContractId));
        request.setBody(body);

        try {
            SecureApiResponse response = this.doPost(request);
            acceptOrderContractService.updateContractStatus(anneSignContractId, ContractStatus.SIGNING);
        } catch (Exception e) {
            log.error("发起合同签署失败:{}", e.getMessage(), e);
            throw SystemRuntimeException.error("发起合同签署失败", e);
        }

    }


}
